import { reactive } from 'vue';
import {url} from './url'
import { getCurrentTime } from './timer'
const files = reactive([
  {
    id: 1,
    name: 'ex1.txt',
    time: 1620626811926,
    type: 'txt',
    size: '8kb',
    path: 'D:/work/note'
  },
  {
    id: 2,
    name: 'note1',
    time: 1620626811926,
    type: 'dir',
    size: '',
    path: 'D:/work/note'
  },
  {
    id: 3,
    name: 'ex2.txt',
    time: 1620626811926,
    type: 'txt',
    size: '8kb',
    path: 'D:/work/note/note1'
  },
  {
    id: 4,
    name: 'work',
    time: 1620626811926,
    type: 'dir',
    size: '',
    path: 'D:'
  },
  {
    id: 5,
    name: 'note',
    time: 1620626811926,
    type: 'dir',
    size: '',
    path: 'D:/work'
  }
])
let cnt = 5

const addFile = function(name){
  cnt = cnt + 1
  const currentTime = getCurrentTime()
  console.log(files);
  const file = {
      id: cnt,
      name: name+'.txt',
      time: currentTime,
      type: 'txt',
      size: '8kb',
      path: url.value 
  }
  files.push(file)
  console.log(files);
}
const addDir = function(name){
  cnt = cnt + 1
  const currentTime = getCurrentTime()
  const file = {
    id: cnt,
    name: name,
    time: currentTime,
    type: 'dir',
    size: '',
    path: url.value
  }
  files.push(file)
}

const deleteFile = function(file){
  console.log(file);
  const index = files.findIndex(item=>item.id === file.id)
  files.splice(index, 1)
  console.log(files);
  
}

const renameFile = function(file, newName){
  const index = files.findIndex(item=>item.id === file.id)
  if (files[index].type==='txt')newName = newName + '.txt'
  files[index].name = newName
  console.log(files)
}

export { 
  addFile,
  addDir,
  deleteFile,
  renameFile
}

export default files